using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Threading;
using Styx;
using Styx.Combat.CombatRoutine;
using Styx.Helpers;
using Styx.Logic;
using Styx.Logic.Combat;
using Styx.Logic.Pathing;
using Styx.Plugins.PluginClass;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

namespace derpilo
{
    public class Antigank : HBPlugin
    {
	
		////////////////////////////
		//////////Variables/////////
		////////////////////////////
		
		// Want to customize this script a little? here are the variables you can play with. Set true or false or change the numbers
		
		// Sit in corpse briefly and pretend to be logging main?
		public bool PRETEND_TO_LOG_MAIN = true;
		// Pretend to log out even if you're max level?
		public bool LOG_MAIN_ON_MAX_LEVEL = false;
		// How long to sit in your corpse before release if PRETEND_TO_LOG_MAIN is true. (in miliseconds)
		public int PRETEND_TO_LOG_MAIN_SLEEP = 30000;
		// The peroid of time between when i got ganked and when i got ganked again that i'll classify it as a repeat gank. (In seconds)
		public int REPEAT_GANK_TIMEOUT = 240;
		// How long to wait before rezzing on repeated ganks (in miliseconds)
		public int DETER_GANKER_SLEEP = 300000;	
		
		// Don't touch these
		private static LocalPlayer Me {	get { return ObjectManager.Me; } }
        public override string Name { get { return "Anti-Ganker"; } }
        public override string Author { get { return "Derpilo"; } }
        public override Version Version { get { return new Version(1, 0, 0, 0); } }
        private static void Log(string message) { Logging.WriteDebug("[RI5] " + message); }
        public bool AntiGankRelease = false;
		public Stopwatch SeenPlayerTimeoutTimer = new Stopwatch();		
		
		public override void Initialize()
        {
			Logging.Write(Color.FromName("Yellow"), "[Anti-Ganker] Loaded version 1.0.0.0");
        }
		
		////////////////////////////
		//////////Main code/////////
		////////////////////////////
		
        public override void Pulse()
        {
			// am i already dead? Or am i in a BG? If so do nothing
			if (!Me.IsAlive && AntiGankRelease || !Me.IsAlive && Me.HasAura("Ghost") || Battlegrounds.IsInsideBattleground || (Battlegrounds.IsInsideBattleground))	return;
			
			//I'm alive, now what?
			if (Me.IsAlive)
			{
				// did i just rez? if so turn off the i just released var and start the repeat gank timer
				if (AntiGankRelease)
				{
					Logging.Write(Color.FromName("Yellow"), "[Anti-Ganker] Keeping an eye out for repeated gank");
					AntiGankRelease = false;
					SeenPlayerTimeoutTimer.Reset();
					SeenPlayerTimeoutTimer.Start();
				}
				// Has the timer elapsed max time? if so then relax
				else if (SeenPlayerTimeoutTimer.IsRunning && SeenPlayerTimeoutTimer.Elapsed.TotalSeconds > REPEAT_GANK_TIMEOUT)
				{
					Logging.Write(Color.FromName("Yellow"), "[Anti-Ganker] I seem to have safe :)");
					SeenPlayerTimeoutTimer.Reset();
					SeenPlayerTimeoutTimer.Stop();
				}
			}
			
			//I'm dead, now what?
			if (!Me.IsAlive && !Me.HasAura("Ghost"))
			{
				// Fix bug when starting bot when dead (it thinks you just died as you rez)
				Thread.Sleep(50);
				if (Me.IsAlive)
				{
					return;
				}
				// Was there an enemy player next to me?
				var EnemyGankers =
				ObjectManager.GetObjectsOfType<WoWPlayer>(false, false).Where(o => !o.Dead && o.IsHostile && !o.IsPet && o.Distance <= 50);	
				if (EnemyGankers.Count() > 0 && !Me.HasAura("Ghost"))
				{
					AntiGankRelease = true;
					// Timer isnt running so this means this is my first gank death
					if (!SeenPlayerTimeoutTimer.IsRunning)
					{
						Logging.Write(Color.FromName("Yellow"), "[Anti-Ganker] I got ganked!");
					}
					// Timer is running so this means i got repeat ganked
					else if (SeenPlayerTimeoutTimer.IsRunning)
					{
						Logging.Write(Color.FromName("Yellow"), "[Anti-Ganker] I got ganked again!");
					}
					Thread.Sleep(1500);
					// Pretend to log main?
					if (Me.Level < 85 && PRETEND_TO_LOG_MAIN || LOG_MAIN_ON_MAX_LEVEL)
					{
						Logging.Write(Color.FromName("Yellow"), "[Anti-Ganker] Pretending to log main, sitting in corpse for a while. (30s default)");
						Thread.Sleep(PRETEND_TO_LOG_MAIN_SLEEP);
					}
					// Release
					Lua.DoString("RunMacroText('/script RepopMe();');");
					Thread.Sleep(1500);
					// First gank? then just go to the corpse.
					if (!SeenPlayerTimeoutTimer.IsRunning)
					{
						Logging.Write(Color.FromName("Yellow"), "[Anti-Ganker] This is the first time i was ganked in a while so i'm corpse running.");
					}
					// Repeat gank? Wait a while.
					else if (SeenPlayerTimeoutTimer.IsRunning)
					{
						Logging.Write(Color.FromName("Yellow"), "[Anti-Ganker] Repeat ganker detected! Waiting a while for my ganker to get bored and leave me alone. (5min default)");
						Thread.Sleep(DETER_GANKER_SLEEP);
						Logging.Write(Color.FromName("Yellow"), "[Anti-Ganker] Wait over, proceeding to my corpse.");
					}
				}
			}
		}
    }
}